
package com.bailiban.aigou.action;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.bailiban.aigou.common.BaseAction;
import com.bailiban.aigou.entity.Product;
import com.bailiban.aigou.entity.ProductType;
import com.bailiban.aigou.service.IProductService;
import com.bailiban.aigou.service.IProductTypeService;
import com.bailiban.aigou.utils.DateUtils;
import com.bailiban.aigou.utils.PageModel;

@Controller
@Scope("prototype")
@Namespace("/admin/product")
public class ProductAction extends BaseAction {
	
	@Autowired
	private IProductTypeService productTypeService;
	
	@Autowired
	private IProductService productService;

	@Action(value = "list", results = {
		@Result(name = SUCCESS, location = SYS_JSP_BASEURL + "admin/product/list.jsp")
	})
	public String list(){
		HttpServletRequest request = ServletActionContext.getRequest();
		super.setPageNo(request.getParameter("pageNo"));
		super.setPageSize(request.getParameter("pageSize"));
		
		//创建查询条件实体
		Product product = new Product();
		product.setProductName(request.getParameter("productName"));
		
		ProductType productType = new ProductType();
		productType.setId(request.getParameter("productType"));
		product.setProductType(productType);
		
		//组装pageModel，调用service
		PageModel<Product> pageModel = new PageModel<Product>();
		pageModel.setPageNo(super.getPageNo());
		pageModel.setPageSize(super.getPageSize());
		pageModel = productService.getProducts(pageModel, product);
		
		request.setAttribute("productPages", pageModel);
		return SUCCESS;
	}
	
	@Action(value = "addPage", results = {
		@Result(name = SUCCESS, location = SYS_JSP_BASEURL + "admin/product/add.jsp")
	})
	public String addPage(){
		HttpServletRequest request = ServletActionContext.getRequest();
		
		List<ProductType> list = productTypeService.getAllProductType();
		request.setAttribute("productTypes", list);
		
		return SUCCESS;
	}
	
	@Action(value = "add")
	public String add(){
		HttpServletRequest request = ServletActionContext.getRequest();
		
		String productName = request.getParameter("productName");
		String productImage = request.getParameter("productImage");
		double price = Double.valueOf(request.getParameter("price"));
		String productTypeId = request.getParameter("productType");
		String productDesc = request.getParameter("productDesc");
		
		ProductType productType = productTypeService.getProductTypeById(productTypeId);
		
		Product product = new Product();
		product.setProductName(productName);
		product.setProductImage(productImage);
		product.setPrice(price);
		product.setProductType(productType);
		product.setProductDesc(productDesc);
		product.setCreateTime(DateUtils.getDateTimeString());
		
		productService.addProduct(product);

		request.setAttribute("url", "admin/product/list");
		return SYS_ACTION_CLOSE;
	}

	@Action(value = "delete")
	public String delete(){
		HttpServletRequest request = ServletActionContext.getRequest();
		String id = request.getParameter("id");
		
		Product product = new Product();
		product.setId(id);
		
		productService.deleteProduct(product);
		
		request.setAttribute("url", "admin/product/list");
		return SYS_ACTION_CLOSE;
	}
}
